<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--操作按钮-->
      <template #toolbar>
        <a-button type="primary" @click="handleAdd">新增</a-button>
        <a-dropdown v-if="hasSelectedRows">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="handleBatchDelete"> <Icon icon="ant-design:delete-outlined"></Icon> 删除 </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作<Icon icon="mdi:chevron-down"></Icon></a-button>
        </a-dropdown>
      </template>
      <!--行内操作-->
      <template #action="{ column, record, index }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <!--添加编辑-->
    <MessageDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="msgContent">
import { BasicTable, TableAction, ActionItem } from '@apps/antd-admin/src/components/Table';
import MessageDrawer from './components/MessageDrawer.vue';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
const [registerDrawer, { openDrawer }] = useDrawer();
import { Api } from './data/message.api';
import { columns, searchFormSchema } from './data/message.data';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { unBindOpenId } from '@/views/business/userManage/appUser/data/api';

const { createMessage, createConfirm } = useMessage();
//初始化Table
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'message-template',
  tableProps: {
    title: '系统公告',
    columns: columns,
    //主键字段
    rowKey: 'msgId',
    //分页列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //查询form配置
    formConfig: {
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 120,
    },
  },
});
const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      icon: 'clarity:note-edit-line',
      onClick: handleEdit.bind(null, record),
    },
    {
      label: '发布公告',
      onClick: handleSendMessage.bind(null, record),
      ifShow: () => {
        return record.msgState == 0;
      },
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        icon: 'ant-design:delete-outlined',
        confirm: handleDelete.bind(null, record),
      },
    },
  ];
}

/**
 * 新增事件
 */
function handleAdd() {
  openDrawer(true, {
    isUpdate: false,
  });
}

/**
 * 编辑事件
 * @param record
 */
function handleEdit(record) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 发布系统公告
 * @param record
 */
function handleSendMessage(record) {
  let msgId = record.msgId;
  createConfirm({
    iconType: 'info',
    title: '发布公告',
    content: '确认发布系统公告吗？',
    onOk: async () => {
      await handlePost(Api.sendMessage, {
        msgId: msgId,
      });
      reload();
    },
  });
}
</script>
